#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_ReponseOfInspectionConductionSchemaModule:1:0:0}'
    #ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'Response'
    
    #Read from inn by XML order

    FunctionalReferenceID = inn.get({'BOTSID':rootelement,ns1+'FunctionalReferenceID':None})
    TypeCode = inn.get({'BOTSID':rootelement,ns1+'TypeCode':None})
    AuthenticatorName = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Authenticator',ns1+'Name':None})
    AuthenticatorCommunicationID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Authenticator'},{'BOTSID':ns1+'Communication',ns1+'ID':None})
    AuthenticatorCommunicationTypeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Authenticator'},{'BOTSID':ns1+'Communication',ns1+'TypeID':None})
    AdditionalInformationStatementCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'AdditionalInformation',ns1+'StatementCode':None})
    AdditionalInformationStatementTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'AdditionalInformation',ns1+'StatementTypeCode':None})
    ControlInspectionEndDateTime = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Control',ns1+'InspectionEndDateTime':None})
    DeclarationOfficeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration',ns1+'DeclarationOfficeID':None})
    DeclarationID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration',ns1+'ID':None})
    DeclarationSequenceNumeric = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration',ns1+'SequenceNumeric':None})
    DeclarationAdditionalDocumentID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration'},{'BOTSID':ns1+'AdditionalDocument',ns1+'ID':None})
    DeclarationConsignmentItemCommodityDescription = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration'},{'BOTSID':ns1+'Consignment'},{'BOTSID':ns1+'ConsignmentItem'},{'BOTSID':ns1+'Commodity',ns1+'Description':None})
    
    DeclarationSubmitterName = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration'},{'BOTSID':ns1+'Submitter',ns1+'Name':None})
    DeclarationSubmitterCommunicationID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration'},{'BOTSID':ns1+'Submitter'},{'BOTSID':ns1+'Communication',ns1+'ID':None})
    DeclarationSubmitterCommunicationTypeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration'},{'BOTSID':ns1+'Submitter'},{'BOTSID':ns1+'Communication',ns1+'TypeID':None})
    
    
    out.ta_info['botskey'] = 'CUSINF-5EM-' + FunctionalReferenceID.strip()
    
    #Write to out by EDIFACT order
    
    out.put({'BOTSID':'UNH','0062':out.ta_info['reference']})
    out.put({'BOTSID':'UNH','S009.0065':'CUSINF'})
    out.put({'BOTSID':'UNH','S009.0052':'S'})
    out.put({'BOTSID':'UNH','S009.0054':'93A'})
    out.put({'BOTSID':'UNH','S009.0051':'KE'})
            
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':TypeCode})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':FunctionalReferenceID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.7365':AdditionalInformationStatementCode,'C529.1131':'204','C529.3055':'KCS'})
    if AdditionalInformationStatementCode == '1':
        out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.7365':AdditionalInformationStatementTypeCode,'C529.1131':'70','C529.3055':'KCS'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'351','C507.2380':ControlInspectionEndDateTime,'C507.2379':'203'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','3227':'50','C517.3225':DeclarationOfficeID[0:3],'C517.1131':'113','C517.3055':'KCS','C519.3223':DeclarationOfficeID[3:5],'C519.1131':'5AB','C519.3055':'KCS'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','3035':'HG','C082.3039':AuthenticatorCommunicationID,'C082.1131':'103','C058.3124#1':AuthenticatorName})    
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','3035':'DT','C082.3039':DeclarationSubmitterCommunicationID,'C082.1131':'103','C058.3124#1':DeclarationSubmitterName})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF','C506.1153':'AFB','C506.1154':DeclarationID,'C506.1156':DeclarationSequenceNumeric})
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF','C506.1153':'AFB','C506.1154':DeclarationID,'C506.1156':DeclarationSequenceNumeric,'C506.4000':DeclarationAdditionalDocumentID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'FTX','4451':'AAA','C108.4440#1':DeclarationConsignmentItemCommodityDescription})
    
    # Loop 0 .. 999
    for rff in inn.getloop({'BOTSID':rootelement},{'BOTSID':ns1+'Declaration'},{'BOTSID':ns1+'Consignment'},{'BOTSID':ns1+'TransportEquipment'},{'BOTSID':ns1+'ID'}):
        DeclarationConsignmentTransportEquipmentID = rff.get({'BOTSID':ns1+'ID','BOTSCONTENT':None})
        
        lou = out.putloop({'BOTSID':'UNH'},{'BOTSID':'RFF'},{'BOTSID':'EQD'})
        lou.put({'BOTSID':'EQD','8053':'CN','C237.8260':DeclarationConsignmentTransportEquipmentID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0074':out.getcount()+1})
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0062':out.ta_info['reference']}) 
    
